package com.ia.bikerecife.speech;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;

public class SpeechHandle {

    private static String MESSAGE             = "Fale o que desejar";

    private static int    REQUEST_RECOGNITION = 101;
    private static int    REQUEST_SYNTHESIS   = 102;
    private static int    REQUEST_CODE        = 101;
    private Intent        mIntent;
    private Activity      mContext;
    private TextToSpeech  mSpeaker;

    public static String  mMessageCache       = "";

    public SpeechHandle(Context context) {

        mContext = (Activity) context;
        mIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        mIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        mIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, MESSAGE);

        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        mContext.startActivityForResult(checkIntent, REQUEST_SYNTHESIS);

        // AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        // int amStreamMusicMaxVol = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        // am.setStreamVolume(AudioManager.STREAM_MUSIC, amStreamMusicMaxVol, 7);
    }

    public OnInitListener initListener = new OnInitListener() {

                                           @Override
                                           public void onInit(int status) {
                                               Log.v("leac", "mSpeaker initialized");
                                               StartSpeechSyntesis(mMessageCache);
                                           }
                                       };

    public boolean IsSpeackInicialized() {
        return (mSpeaker != null);
    }

    public void StartSpeechSyntesis(String message) {

        if (!IsSpeackInicialized()) {
            mMessageCache = message;
            return;
        }

//        mSpeaker.setPitch(0.1f);
//        mSpeaker.setSpeechRate(0.9f);
        mSpeaker.setLanguage(new Locale("pt-BR"));

        mSpeaker.speak("", TextToSpeech.QUEUE_FLUSH, null);

        for (String string : message.split(" ")) {
            try {
                Thread.sleep(100);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            mSpeaker.speak(string, TextToSpeech.QUEUE_ADD, null);
        }

        mMessageCache = "";
    }

    public String onActivityResult(int requestCode, int resultCode, Intent data) {

        ArrayList<String> textMatchList;
        try {
            if (requestCode == REQUEST_RECOGNITION) {
                textMatchList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                String information = textMatchList.get(0);
                return information;
            }
            else if (requestCode == REQUEST_SYNTHESIS) {
                if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                    mSpeaker = new TextToSpeech(mContext, initListener);

                }
                else {
                    Intent installIntent = new Intent();
                    installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                    mContext.startActivity(installIntent);
                }
            }
        }
        catch (Exception e) {
            // mEditText.setText(NOT_REGNIZED);
            //SayIt(NOT_REGNIZED);
            return "";
        }
        return "";
    }

    public String CallSpeechRecognizer() {
        mContext.startActivityForResult(mIntent, REQUEST_CODE);
        return null;
    }

    public void onDestroy() {
        mSpeaker.stop();
        mSpeaker.shutdown();
    }
}
